<?php
if (!defined('__TYPECHO_ROOT_DIR__')) exit;
define('__PLUGIN_ROOT__', __DIR__);
/**
 * <strong style="color:#000000;">Gm账号统一登录</strong>
 * 
 * @package GmSSO
 * @author Gm
 * @version 1.0.0
 * @update: 2024年8月27日13:49:37
 * @link //www.gmya.net
 */
class GmSSO_Plugin implements Typecho_Plugin_Interface
{
    public static $baseURL = "https://login.gmya.net";
    public static $panel = 'GmSSO/console.php';
    /**
     * 激活插件方法,如果激活失败,直接抛出异常
     * 
     * @access public
     * @return void
     * @throws Typecho_Plugin_Exception
     */
    public static function activate()
    {
        Helper::addRoute('GmSSO', '/id/callback', 'GmSSO_Action', 'callback');
        try {
            $db = Typecho_Db::get();
            $prefix = $db->getPrefix();
            $res = $db->query("SHOW COLUMNS FROM {$prefix}users LIKE 'gm_user_id';");
            $sql = "ALTER TABLE `blog`.`{$prefix}users` 
ADD COLUMN `gm_user_id` bigint(16) NULL;";
            $db->query($sql);
            return '插件安装成功!';
        } catch (Typecho_Db_Exception $e) {
            if ('42S01' == $e->getCode()) {
                return '插件安装成功!';
            }
        }
    }
    
    /**
     * 禁用插件方法,如果禁用失败,直接抛出异常
     * 
     * @static
     * @access public
     * @return void
     * @throws Typecho_Plugin_Exception
     */
    public static function deactivate(){
        Helper::removeRoute('GmSSO');
        return '插件卸载成功';
    }
    
    /**
     * 获取插件配置面板
     * 
     * @access public
     * @param Typecho_Widget_Helper_Form $form 配置面板
     * @return void
     */
    public static function config(Typecho_Widget_Helper_Form $form){
        $title = new Typecho_Widget_Helper_Layout('div', array('class=' => 'typecho-page-title'));
        $title->html('<h2>插件配置</h2>');
        $form->addItem($title);
        $client_id = new Typecho_Widget_Helper_Form_Element_Text('client_id', null, '', _t('Client ID'), '开放应用 Client ID');
        $form->addInput($client_id);
        $client_secret = new Typecho_Widget_Helper_Form_Element_Text('client_secret', null, '', _t('Client Secret'), '开放应用 Client Secret');
        $form->addInput($client_secret);
    }
    
    /**
     * 个人用户的配置面板
     * 
     * @access public
     * @param Typecho_Widget_Helper_Form $form
     * @return void
     */
    public static function personalConfig(Typecho_Widget_Helper_Form $form){}
    
    public static function login()
    {
        $plugin = Typecho_Widget::widget('Widget_Options')->plugin('GmSSO');
        return self::$baseURL."/oauth/authorize?client_id={$plugin->client_id}&redirect_uri=".urlencode(Typecho_Common::url('/id/callback?ref='.self::ref(), Helper::options()->index))."&state=".time().rand(100000,999999);
    }
    
    private static function ref(){
        $sys_protocal = isset($_SERVER['SERVER_PORT']) && $_SERVER['SERVER_PORT'] == '443' ? 'https://' : 'http://';
        $php_self = $_SERVER['PHP_SELF'] ? $_SERVER['PHP_SELF'] : $_SERVER['SCRIPT_NAME'];
        $path_info = isset($_SERVER['PATH_INFO']) ? $_SERVER['PATH_INFO'] : '';
        $relate_url = isset($_SERVER['REQUEST_URI']) ? $_SERVER['REQUEST_URI'] : $php_self.(isset($_SERVER['QUERY_STRING']) ? '?'.$_SERVER['QUERY_STRING'] : $path_info);
        $url = urlencode($sys_protocal.(isset($_SERVER['HTTP_HOST']) ? $_SERVER['HTTP_HOST'] : '').$relate_url);
        return $url;
    }
}
